var mongdb = require('./db');

function User(user){
	this.name = user.name;
	this.password = user.password;
}

module.exports = User;

User.prototype.save = function save(callback){
	var user = {
		name:this.name,
		password:this.password
	};

	mongdb.open(function(err, db){
		if(err){
			return callback(err);
		}

		db.collection('users', function(err, collection){
			if(err){
				mongdb.close();
				return callback(err);
			}

			collection.ensureIndex('name', {unique:true});
			collection.insert(user, {safe:true}, function(err, user){
				mongdb.close();
				callback(err, user);
			});
		});
	});
}

User.get = function get(username, callback){

	mongdb.open(function(err, db){
		if(err){
			return callback(err);
		}

		db.collection('users', function(err, collection){
			if(err){
				mongdb.close();
				return callback(err);
			}
			collection.findOne({name:username}, function(err, doc){
				mongdb.close();
				if(doc){
					var user = new User(doc);
					callback(err, user);
				} else {
					callback(err, null);
				}
			})
		});
	})
}